<?php

namespace app\mobile\model;

use app\common\helper\Time;
use app\common\model\RougeShare;
use app\common\model\RougeShareLog;
use app\common\model\RougeShareSuccessLog as ShareSuccessLog;
use app\common\model\RougeUser;
use think\Exception;
use think\Session;

/**
 * 通过分享链接成功导入用户进驻平台的记录
 *
 * Class Distribution
 * @package app\mobile\model
 */
class RougeShareSuccessLog extends ShareSuccessLog{


    /**
     * 通过分享进入平台的分享佣金及分享记录入库
     *
     * @param $share_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws Exception
     */
    public function addShare($share_id) {
        $shareLogM = new RougeShareLog();
        $share_log = $shareLogM->where(['id' => $share_id, 'statu' => 1])->find();
        if (empty($share_log)) { // 没有分享日志的记录
            return false;
        }
        $share_log = $share_log->getData();

        // 判断是否是有效的拉人
        $sessionM = new Session();
        $current_user_id = $sessionM->get('user_id');
        //var_dump($current_user_id);die;
        $userM = new RougeUser();
        $share_user_nickname = $userM->where(['openid' => $share_log['openid']])->find()->getData('nickname');// 分享人的昵称
        $current_user = $userM->where(['id' => $current_user_id])->find()->getData();
        if ($current_user['create_time'] <= $share_log['create_time']) { // 用户早于分享已经入驻平台，拉人无效
            return false;
        }
        // 如果已经有记录则也是无效的拉人
        $has_record = $this->where(['by_openid' => $current_user['by_openid']])->find();
        if (!empty($has_record)) {
            return false;
        }

        // 分享配置读取
        $shareM = new RougeShare();
        $share = $shareM->where(['statu' => 1, 'uniacid' => $share_log['uniacid']])->find();
        if (empty($share)) {
            return false;
        }
        $share = $share->getData();
        // 读取当天的分享成功记录总条数
        $today_start = Time::getTodayStart();
        $today_end = Time::getTodayEnd();
        $today_count = $this->where('created_time', 'between', [$today_start, $today_end])->where(['openid' => $share_log['openid']])->count();
        if ($share['num'] <= $today_count) { // 如果当天的分享次数已达上限则返回，不做处理
            //die('num');
            return false;
        }

        // 新增成功拉人的分享记录
        $uniacid = Session::get('uniacid');
        $data = [
            'share_log_id' => $share_id,
            'created_time' => date('Y-m-d H:i:s', time()),
            'openid' => $share_log['openid'],
            'by_openid' => $current_user['openid'],
            'share_money' => $share['price'],
            'uniacid' => $uniacid,
            'by_nickname' => $current_user['nickname'],
            'nickname' => $share_user_nickname,
        ];
        if (!$this->save($data)) {
            return false;
        }

        // 修改分享者的个人的信息（资金变动）
        $userM->where(['openid' => $share_log['openid']])->setInc('balance', $share['price']);
        return true;
    }

}